package com.work.mdass.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.work.mdass.domain.Course;
import com.work.mdass.domain.Result;
import com.work.mdass.service.CourseService;
import com.work.mdass.util.CourseResultUtil;

/*
 * 课程控制器
 */
@Controller
@RequestMapping("/admin/course")
public class CourseController {

	@Autowired
	private CourseService courseService;
	
	/*
	 * 获得所有课程
	 * 
	 */
	@GetMapping("/listCourse")
	@ResponseBody
	public List<Course> getAllCourse(){

		return courseService.getAllCourse();
	}
	
	/*
	 * 添加一门课程
	 */
	@PostMapping("/save")
	@ResponseBody
	public Result<Course> save(@Valid Course course,BindingResult bindingResult){
		
		if(bindingResult.hasErrors()){
			
			return CourseResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage());
		}
		
		return CourseResultUtil.success(courseService.addCourse(course));
	}
	/*
	 * 修改课程信息
	 */
	@PostMapping(value ="/updata")
	@ResponseBody
	public Result<Course> updata(@Valid Course course,BindingResult bindingResult){
		if(bindingResult.hasErrors()) {
			return CourseResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage());
		}
		
		return CourseResultUtil.success(courseService.updataCourse(course));
	}
	/*
	 * 删除课程
	 */
	@DeleteMapping(value = "/delete/{id}")
	@ResponseBody
	public Result<Course> delete(@PathVariable("id") Integer id){
		
		courseService.deleteById(id);
		return CourseResultUtil.success();
	}
	/*
	 * 根据任课老师查找课程
	 */
	@PostMapping("/getByTName")
	@ResponseBody
	public Map<Object,Object> getCourseByTName(@RequestParam(required = false) String tName){
		return courseService.findByTName(tName);
	}
	/*
	 * 通过id获取课程信息
	 */
	@PostMapping("/getById/{id}")
	@ResponseBody
	public Result<Course> getCourseById(@PathVariable("id") Integer id){
		
		return CourseResultUtil.success(courseService.getCourse(id));
	}
	
	/*
	 * 通过关键字获得课程信息
	 */
	@GetMapping("/byKeyWord")
	@ResponseBody
	public Map<Object,Object> getByKeyWord(@RequestParam(required = false) String keyWord){
		
		return courseService.getByKeyWords(keyWord);
	}
	/*
	 * 通过两个字段关联查找
	 */
	@PostMapping("/highcharts")
	@ResponseBody
	public Map<Object,Object> forHighcharts(@RequestParam(required = false)String tName,
			@RequestParam(required = false)String classTrem){
		return courseService.forHighcharts(tName, classTrem);
	}
}
